package com.attendance.sz.up.service;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yc.utilslib.asynctask.WebApiRequest;
import com.yc.utilslib.utils.ApiUtils;
import com.yc.utilslib.utils.GZipUtils;
import com.yc.utilslib.utils.StringUtils;

import com.attendance.sz.up.entity.up.Error;

/**
 * 作者：TanTun
 * 时间：2017/6/13
 * 邮箱：32965926@qq.com
 * 描述：
 */

public class QueryPhotoService implements IService<Long, Bitmap> {
    private Long empId;
    private OnServiceResultListener<Long, Bitmap> mOnServiceResultListener;
    private OnServiceErrorListener<Long> mOnServiceErrorListener;

    /**
     * @param onRequestResultListener 请求结果通知
     */
    public void setOnRequestResultListener(
            OnServiceResultListener<Long, Bitmap> onRequestResultListener) {
        this.mOnServiceResultListener = onRequestResultListener;
    }

    /**
     * @param onRequestErrorListener 请求结果通知
     */
    public void setOnServiceErrorListener(
            OnServiceErrorListener<Long> onRequestErrorListener) {
        this.mOnServiceErrorListener = onRequestErrorListener;
    }

    public boolean query(Long id) {

        boolean ret = false;
        empId = id;

        WebApiRequest<Bitmap, Error> request = new WebApiRequest<>();
        request.setAsyncTaskSuccess(new WebApiRequest.AsyncTaskSuccess<Bitmap>() {

            @Override
            public Bitmap parsing(String data) {
                Bitmap image = null;
                if (!StringUtils.isNullOrEmpty(data)) {
                    byte[] buffer = GZipUtils.decompressForGzip(data);
                    if (buffer != null && buffer.length > 0) {
                        image = BitmapFactory.decodeByteArray(buffer, 0, buffer.length);
                    }
                }

                return image;
            }

            @Override
            public void success(Bitmap image) {
                if (mOnServiceResultListener != null) {
                    mOnServiceResultListener.onResult(empId, image);
                }
            }
        });
        request.setAsyncTaskError(new WebApiRequest.AsyncTaskError<Error>() {
            @Override
            public Error parsing(String str) {

                Error err = null;

                if (!TextUtils.isEmpty(str)) {
                    try {
                        Gson gson = new Gson();
                        err = gson.fromJson(str,
                                new TypeToken<Error>() {
                                }.getType());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                return err;
            }

            @Override
            public void error(Error obj) {
                if (obj != null) {
                    if (obj.getErrors() != null && obj.getErrors().size() > 0) {
                        mOnServiceErrorListener.onError(empId, obj.getErrors().get(0).getMessage());
                    } else {
                        mOnServiceErrorListener.onError(empId, obj.getMessage());
                    }
                } else {
                    mOnServiceErrorListener.onError(empId, "未知错误");
                }
            }

            @Override
            public void error(String msg) {
                mOnServiceErrorListener.onError(empId, msg);
            }
        });

        try {
            request.setMethod("GET");
            request.setHead("accept", "application/json");
            request.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
                    ApiUtils.API + "/api/up/v6/employees/photos/" + empId, StringUtils.EMPTY);
            ret = true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ret;
    }
}
